//
const ssr = require('./ssr/svg/namespace/SVGNamespace');

cc.Class({
    extends: cc.Component,

    properties: {
        svgNode: cc.Node,
        logView: cc.ScrollView,
    },

    onLoad () {
        cc.debug.setDisplayStats(true);
        this._testcase = cc.director.getScene().name;
        cc.Canvas.instance.node.getChildByName("Title").getComponent(cc.Label).string = "Testcase - " + this._testcase;
    },

    start () {
        this._strokeAnimateSpeed = 0.1 * 0.1;
        //
        this._writerComponent = this.svgNode.getComponent(ssr.SVG.WriterComponent);
        this._writerComponent.drawFrame();
        this._writerComponent.drawGrid();
        this._writerComponent.drawMedians();
        this._writerComponent.drawHanzi();
    },

    onEditingReturn (event) {
        let hanzi = event.textLabel.string;
        let self = this;
        this._writerComponent.load(hanzi, function (err) {
            self._writerComponent.resetHanziRender()
            if (!err) {
                self._writerComponent.drawHanzi();
            }
        });
    },

    onStrokeColor (event) {
        this._writerComponent.strokeColor = cc.color(255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255);
    },

    onStrokeWidth (event) {
        this._writerComponent.strokeWidth = (50 * event.progress);
    },

    onStrokeRandom (event) {
        this._writerComponent.randomStroke();
    },

    onStrokeNext (event) {
        this._writerComponent.stroke();
    },

    onStrokeReset (event) {
        this._writerComponent.resetStroke();
    },

    onStrokeAnimate (event) {
        this._writerComponent.animateStroke(null, this._strokeAnimateSpeed);
    },

    onStrokeAnimateRandom (event) {
        this._writerComponent.randomAnimateStroke(this._strokeAnimateSpeed);
    },

    onStrokeAnimateReset (event) {
        this._writerComponent.resetStroke();
    },

    onStrokeAnimateSpeed (event) {
        this._strokeAnimateSpeed = (event.progress * 0.1);
    },

    onCbOneStrokeStart (event, data) {
        this._addLog("onCbOneStrokeStart");
        this._addLog(JSON.stringify(data));
    },
    onCbOneStrokeMove (event, data) {
        this._addLog("onCbOneStrokeMove");
    },
    onCbOneStrokeEnd (event, data) {
        this._addLog("onCbOneStrokeEnd");
        this._addLog(JSON.stringify(data));
    },
    onCbOneMedianStart (event, data) {
        this._addLog("onCbOneMedianStart");
        this._addLog(JSON.stringify(data));
    },
    onCbOneMedianMove (event, data) {
        this._addLog("onCbOneMedianMove");
        this._addLog(JSON.stringify(data));
    },
    onCbOneMedianEnd (event, data) {
        this._addLog("onCbOneMedianEnd");
        this._addLog(JSON.stringify(data));
    },
    onCbStrokeStart (event, data) {
        this._addLog("onCbStrokeStart");
    },
    onCbStrokeEnd (event, data) {
        this._addLog("onCbStrokeEnd");
    },

    //
    _addLog (text) {
        let node = new cc.Node();
        node.anchorX = 0;
        node.anchorY = 1;
        node.color = cc.Color.BLACK;
        node.x = - 290;
        let comp = node.addComponent(cc.Label);
        comp.string = text;
        comp.fontSize = 20;
        node.parent = this.logView.content;
        //
        this.logView.scrollToBottom();
    },

    onBack () {
        cc.director.loadScene('TestList');
    }

});
